<?php
 		include_once('opendb.php');
 		include_once('config.php');

 		if($db){
 			echo "Connected <br />";
		}

		$exists = $db->query("SELECT 1 FROM animals LIMIT 0");
		if($exists) {
			include_once('class.petfinder.php');

			$pet = new Petfinder;
			$pet->setKey($key);
			$pet->setPass($pass);

			$pet->getToken();

			$pet->set( array('cache_enable' => FALSE) );

			$config = array('id'=>$shelterNum);
			$pet->initialize($config);
			$data = $pet->shelterGetPets();

			$query = "UPDATE animals SET name=?, breeds=?, sex=?, size=?, age=?, status=?, lastUpdate=?, options=?, description=?, pic1=?, pic2=?, pic1Thumb=?, pic2Thumb=? WHERE petID = ?";

			for ($i=0; $i < count($data); $i++) {

				$petID = $data[$i]['id'];
				$name = $data[$i]['name'];
				$breeds = $data[$i]['breeds'];
				$sex = $data[$i]['sex'];
				$size = $data[$i]['size'];
				$age = $data[$i]['age'];
				$status = $data[$i]['status'];
				$lastUpdate = $data[$i]['lastUpdate'];
				$options = $data[$i]['options'];
				$desc = $data[$i]['description'];
				$pic1 = $data[$i]['photo']['1']['pn'];
				$pic1Thumb = $data[$i]['photo']['1']['pnt'];
				$pic2 = $data[$i]['photo']['2']['pn'];
				$pic2Thumb = $data[$i]['photo']['2']['pnt'];

				$stmt = $db->stmt_init();

				if($stmt->prepare($query)) {

					$stmt->bind_param("sssssssssssssi", $name, $breeds, $sex, $size, $age, $status, $lastUpdate, $options, $desc, $pic1, $pic2, $pic1Thumb, $pic2Thumb, $petID);

					$stmt->execute();

					$stmt->fetch();

					$stmt->close();
				}
        echo $name . " updated, ";
			}
		}else {
			echo "table not found please import.";
		}

        echo "<br />Database updated<br />";

		include_once('closedb.php');
?>
